import { defineStore } from "pinia";
import router from "@/router";
import { TagClass } from "#/index";
import type { RouteLocationNormalizedLoaded } from 'vue-router';

export const useTagStore = defineStore({
    id: "TagsStore",
    persist: true,//开启持久化
    state: () => ({tagList: [new TagClass('/index/dashboard', 'home', 'dashboard')]}),
    actions: {
        /**
         * 添加标签
         * @param route 路由对象
         * @param index 要添加的位置，默认添加到末尾
         * @returns
         */
        pushTag(route: RouteLocationNormalizedLoaded, index: number = -1) {
            const { fullPath, meta, name } = route
            if ("redirect" === name) { //过滤重定向页面
                return
            }
            let openTags = this.tagList;
            let findTag = openTags.find((v: TagClass) => v.fullPath == fullPath);
            if (!findTag) {
                var i = index > -1 ? index : openTags.length
                openTags.splice(i, 0, new TagClass(fullPath, meta.title as string, name as string))
            }
        },
        
        /**
         * 删除标签
         * @param index 标签索引位置
         */
        removeTag(index: number) {
            let openTags = this.tagList;
            let tag = openTags[index];
            let route = router.currentRoute.value; //当前路由
            //删除当前激活的标签，优先显示后面的标签，如果没有显示前面的标签
            if (route.fullPath == tag.fullPath) {
                const { fullPath } = (openTags[index + 1] || openTags[index - 1])
                router.push(fullPath);
            }
            openTags.splice(index, 1)
        },

        /**
         * 根据路径删除标签
         * @param fullPath 要删除的路由路径
         */
        removeTagByPath(fullPath: string) {
            let openTags = this.tagList;
            let index = openTags.findIndex(item => item.fullPath === fullPath);
            if (-1 != index) {
                this.removeTag(index);//异步调用actions的removeTag
            }
        },

        /**
         * 点击标签
         * @param index 标签索引位置
         */
        clickTag(index: number) {
            const { fullPath } = this.tagList[index]
            let route = router.currentRoute.value;//当前路由
            if (fullPath != route.fullPath) {
                router.push(fullPath)
            }
        },

        /**
         * 关闭所有标签
         */
        colseAll() {
            this.tagList.splice(1);
            router.push({ name: "dashboard" })
        },

        /**
         * 删除索引所在的所有右侧标签
         * @param index 标签索引位置
         */
        closeRight(index: number) {
            let openTags = this.tagList;
            let activeIndex = openTags.findIndex(item => item.fullPath === router.currentRoute.value.fullPath)
            openTags.splice(index + 1)
            if (activeIndex > index) {//当前页面被删除，显示右击菜单也
                const { fullPath } = openTags[index]
                router.push(fullPath)
            }
        },

        /**
         * 刷新
         * 标签索引位置
         */
        refresh(index: number) {
            const { fullPath } = this.tagList[index]
            router.push('/index/redirect/' + fullPath)
        }
    }
})